﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace BoKeYuanApp.BLL
{
    /// <summary>
    /// <para>统计 https://www.cnblogs.com/aggsite/AggStats</para>
    /// <para>推荐博客 https://www.cnblogs.com/aggsite/UserStats</para>
    /// <para>推荐博客 https://www.cnblogs.com/xinyf/mvc/blog/sidecolumn.aspx?blogApp=xinyf</para> 
    /// </summary>
    public class BoKeYuanHelper
    {
        private string HttpGetPersonStats()
        {
            string httpUrl1 = "https://www.cnblogs.com/xinyf/mvc/blog/sidecolumn.aspx?blogApp=xinyf";
            httpUrl1 = "https://www.cnblogs.com/xinyf/ajax/sidecolumn.aspx";
            string htmlStr = new HttpHelp().HttpWebRequestGet(httpUrl1);
            return htmlStr;

        }
        private string HttpGetAggStats()
        {
            string httpUrl1 = "https://www.cnblogs.com/aggsite/AggStats";
            string htmlStr = new HttpHelp().HttpWebRequestGet(httpUrl1);
            return htmlStr;
        }

        public Tuple<int, int, int, int> ParseAggStats()
        {
            bool isfind = false;
            string returnstr = string.Empty;
            int index = 0;
            while (!isfind)
            {
                returnstr = HttpGetAggStats();
                index++;
                if (returnstr.Contains("<ul><li>博客"))
                {
                    isfind = true;
                }
                Console.WriteLine(index);
            }
            //<ul><li>博客 - <span>454322</span></li><li>随笔 - <span>6076429</span></li><li>文章 - <span>634171</span></li><li>评论 - <span>3150217</span></li></ul>

            {
                string p = @"<li>博客\s*-\s*<span>([\d\.]+)</span></li><li>随笔\s*-\s*<span>([\d\.]+)</span></li><li>文章\s*-\s*<span>([\d\.]+)</span></li><li>评论\s*-\s*<span>([\d\.]+)</span></li>";
                string ps = @"<li>博客\s*-\s*<span>\d*</span></li>";
                Regex reg = new Regex(p);
                Match match = reg.Match(returnstr);
                string value0 = match.Groups[0].Value;
                string strBlog = match.Groups[1].Value;
                string strJotting = match.Groups[2].Value;
                string strArticle = match.Groups[3].Value;
                string strComment = match.Groups[4].Value;
                string strvalue5 = match.Groups[5].Value;
                Console.WriteLine(match);

                int blog = int.Parse(match.Groups[1].Value);
                int jotting = int.Parse(match.Groups[2].Value);
                int article = int.Parse(match.Groups[3].Value);
                int comment = int.Parse(match.Groups[4].Value);

                return new Tuple<int, int, int, int>(blog, jotting, article, comment);

            }

            {
                string p = @"<li>随笔\s*-\s*<span>([\d\.]+)</span></li>";
                string ps = @"<li>随笔\s*-\s*<span>\d*</span></li>";
                Regex reg = new Regex(p);

                Match match = reg.Match(returnstr);
                string value0 = match.Groups[0].Value;
                string value = match.Groups[1].Value;
                string value2 = match.Groups[2].Value;
                Console.WriteLine(match);
            }
        }
        public Tuple<int, int> ParsePersonStats()
        {
            bool isfind = false;

            //<ul>
            // <li class="liScore">
            // 积分 -	16450
            // </li>
            // <li class="liRank">
            // 排名 -	28720
            // </li>
            // </ul>

            // <H3>积分与排名</H3>
            // <ul>
            // <li>
            // 积分 -
            // 17985
            // <li>
            // 排名 -
            // 27736
            // </li>
            // </ul>
            //
            //
            //因为不同用户使用的皮肤不同，获取到的排名和积分字符串也不同
            string p = @"<li.*>\s*积分\s*-\s*([\d\.]+)\s*[</li>]*\s*<li.*>\s*排名\s*-\s*([\d\.]+)\s*</li>";
            //string p = @"<li class=""liScore"">\s*积分\s*-\s*([\d\.]+)\s*</li>\s*<li class=""liRank"">\s*排名\s*-\s*([\d\.]+)\s*</li>";
            //string ps = @"<li class=""liScore"">\s*积分\s*-\s*([\d\.]+)\s*</li>";
            Regex reg = new Regex(p);

            string returnstr = string.Empty;
            int index = 0;
            while (!isfind)
            {
                returnstr = HttpGetPersonStats();
                index++;
                if (reg.IsMatch(returnstr))
                {
                    isfind = true;
                }
                Console.WriteLine(index);
            }

            Match match = reg.Match(returnstr);
            string value0 = match.Groups[0].Value;
            string strScore = match.Groups[1].Value;
            string strRanking = match.Groups[2].Value;
            int score = int.Parse(match.Groups[1].Value);
            int rinking = int.Parse(match.Groups[2].Value);
            Console.WriteLine(match);
            return new Tuple<int, int>(score, rinking);
        }
        public void demo()
        {

            var method = System.Reflection.MethodBase.GetCurrentMethod();
            string str = "命名空间名:" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "\n";
            //取得当前方法类全名 包括命名空间    
            str += "类名:" + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + "\n";
            //取得当前方法名    
            str += "方法名:" + System.Reflection.MethodBase.GetCurrentMethod().Name + "\n"; str += "\n";

            //父方法
            System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
            System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();

            //取得父方法命名空间    
            str += mb.DeclaringType.Namespace + "\n";

            //取得父方法类名    
            str += mb.DeclaringType.Name + "\n";

            //取得父方法类全名    
            str += mb.DeclaringType.FullName + "\n";

            //取得父方法名    
            str += mb.Name + "\n";
            Console.WriteLine(str);

        }
    }

   
}
